﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotvaBot.Processor;
using BotvaBot.Configuration.Personaj;
using BotvaBot.RequestChains;
using System.Threading;
using BotvaBot.Configuration;

namespace BotvaBot
{
    public class BotvaWorkProcessesManager
    {
        public PersonajCfg Personaj { get; set; }



        public BotvaWorkProcessesManager(PersonajCfg personaj)
        {
            Personaj = personaj;

            CountForMonster = 0;
        }



        protected int CountForMonster { get; set; }

        public bool WorkInKarier(object args, ILoggable log)
        {
            bool result = false;

            log.OnSendLogMessage("Work to Karier is Started");

            WorkInKarierChain ch = new WorkInKarierChain();
            ch.Personaj = Personaj;
            ch.RunChain();

            log.OnSendLogMessage("Work to Karier is Finished");

            if (ch.IsSuccessfulyInvoked.HasValue && (!ch.IsSuccessfulyInvoked.Value))
            {
                log.OnSendLogMessage("Work to Karier was Failed");
                return result;
            }

            log.OnSendLogMessage("Waiting in Karier is Started");
            Thread.Sleep(5 * 60 * 1000 + 5 * 1000);
            log.OnSendLogMessage("Waiting in Karier is Finished");

            log.OnSendLogMessage("Try to Get a Cristall is Started");

            TryGetCristalChain ch1 = new TryGetCristalChain();
            ch1.Personaj = Personaj;
            ch1.RunChain();

            log.OnSendLogMessage("Try to Get a Cristall is Finished");

            if (ch1.IsSuccessfulyInvoked.HasValue && ch1.IsSuccessfulyInvoked.Value)
            {
                result = true;
                log.OnSendLogMessage("Try to Get a Cristall was Success");
            }
            else
            {
                log.OnSendLogMessage("Try to Get a Cristall was Failed");
            }

            Thread.Sleep(2000);

            if (CountForMonster == 0)
            {
                try
                {
                    log.OnSendLogMessage("Try Fight With Monster Start");
                    FightWithMonsterChain ch2 = new FightWithMonsterChain();
                    ch2.Personaj = Personaj;
                    ch2.RunChain();
                    log.OnSendLogMessage("Try Fight With Monster Finish");
                }
                catch
                {
                    log.OnSendLogMessage("Try Fight With Monster Failed");
                }
            }
            CountForMonster++;
            if (CountForMonster == Config.Custom.MonsterFightDelitel)
                CountForMonster = 0;

            log.OnSendLogMessage("Service MainLoop is Finished");
            
            return result;
        }

        public bool SendBoat(object args, ILoggable log)
        {
            bool result = false;

            log.OnSendLogMessage("Sending Start");

            SendBoatChain ch = new SendBoatChain();
            ch.Personaj = Personaj;
            ch.RunChain();

            log.OnSendLogMessage("Sending Stop");

            result = ch.IsSuccessfulyInvoked.HasValue && ch.IsSuccessfulyInvoked.Value;

            return result;
        }

        public bool SendLetun(object args, ILoggable log)
        {
            bool result = false;

            log.OnSendLogMessage("Sending Start");

            SendLetunToBigAdventureChain ch = new SendLetunToBigAdventureChain();
            ch.Personaj = Personaj;
            ch.GildiaId = Personaj.WarGildia.GildiaId;
            ch.LetunId = Personaj.WarGildia.LetunId;

            ch.RunChain();

            log.OnSendLogMessage("Sending Stop");

            result = ch.IsSuccessfulyInvoked.HasValue && ch.IsSuccessfulyInvoked.Value;
            return result;
        }

        public bool Drink(object args, ILoggable log)
        {
            return true;
        }

        public bool Test(object args, ILoggable log)
        {
            log.OnSendLogMessage("Action done");
            return true;
        }
    }
}
